"use strict";
const { Model } = require("sequelize");
module.exports = (sequelize, DataTypes) => {
  class Chapter extends Model {
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate(models) {
      // define association here
      models.Chapter.belongsTo(models.Course, {foreignKey: 'CourseId', as: "course"})
    }
  }
  Chapter.init(
    {
      name: {
        type: DataTypes.STRING,
        allowNull: false,
        validate: {
          notNull: '不能为null',
          notEmpty: '请输入章节名',
        }
      },
      content: DataTypes.TEXT,
      videoUrl: DataTypes.STRING,
      introduce: DataTypes.TEXT,
      CourseId: {
        type: DataTypes.INTEGER,
        allowNull:false,
        validate: {
          notNull: '所属文章不能为null',
          notEmpty: '所属文章不能为空',
        }
      },
      rank: {
        type: DataTypes.INTEGER,
        allowNull:false,
        validate: {
          notNull: '排序不能为null',
          notEmpty: '排序不能为空',
        }
      },
    },
    {
      sequelize,
      modelName: "Chapter",
    }
  );
  return Chapter;
};
